//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

using System.Net;
using System.IO;

using Microsoft.Research.ScientificWorkflow;

namespace Microsoft.Research.ScientificWorkflow.Activities
{
    [Name("COVE Message", "COVE Message")]
    [Description("Sends a message to COVE with an optional file")]
    [WorkflowCategory("NetCdf")]
    public partial class CoveMessage : System.Workflow.ComponentModel.Activity
    {
        #region Property Definitions
        // Required input property
        public static DependencyProperty HostNameProperty =
            DependencyProperty.Register("HostName", typeof(string), typeof(CoveMessage),
            new PropertyMetadata("localhost:11223", DependencyPropertyOptions.Default));
        // Optional input properties
        public static DependencyProperty TypeProperty =
            DependencyProperty.Register("Type", typeof(string), typeof(CoveMessage),
             new PropertyMetadata("DataSets", DependencyPropertyOptions.Optional));
        public static DependencyProperty DataNameProperty =
            DependencyProperty.Register("DataName", typeof(string), typeof(CoveMessage),
             new PropertyMetadata(string.Empty, DependencyPropertyOptions.Optional));
        public static DependencyProperty DataTextProperty =
            DependencyProperty.Register("DataText", typeof(string), typeof(CoveMessage),
             new PropertyMetadata(string.Empty, DependencyPropertyOptions.Optional));
        public static DependencyProperty LinkProperty =
            DependencyProperty.Register("Link", typeof(string), typeof(CoveMessage),
             new PropertyMetadata(string.Empty, DependencyPropertyOptions.Optional));
        public static DependencyProperty AutoProperty =
            DependencyProperty.Register("Auto", typeof(bool), typeof(CoveMessage),
             new PropertyMetadata(false, DependencyPropertyOptions.Optional));

        [RequiredInputParam]
        [Name("HostName")]
        [Description("Required host name (e.g. 'localhost:11223')")]
        public string HostName
        {
            get { return ((string)(base.GetValue(CoveMessage.HostNameProperty))); }
            set { base.SetValue(CoveMessage.HostNameProperty, value); }
        }

        [OptionalInputParam]
        [Name("Type")]
        [Description("Type of layer data for COVE")]
        public string Type
        {
            get { return ((string)(base.GetValue(CoveMessage.TypeProperty))); }
            set { base.SetValue(CoveMessage.TypeProperty, value); }
        }

        [OptionalInputParam]
        [Name("DataName")]
        [Description("Name of the dataset")]
        public string DataName
        {
            get { return ((string)(base.GetValue(CoveMessage.DataNameProperty))); }
            set { base.SetValue(CoveMessage.DataNameProperty, value); }
        }

        [OptionalInputParam]
        [Name("DataText")]
        [Description("Descriptive text to send to COVE to display")]
        public string DataText
        {
            get { return ((string)(base.GetValue(CoveMessage.DataTextProperty))); }
            set { base.SetValue(CoveMessage.DataTextProperty, value); }
        }

        [OptionalInputParam]
        [Name("Link")]
        [Description("Link to a data file")]
        public string Link
        {
            get { return ((string)(base.GetValue(CoveMessage.LinkProperty))); }
            set { base.SetValue(CoveMessage.LinkProperty, value); }
        }

        [OptionalInputParam]
        [Name("Auto")]
        [Description("Whether data should be automatically updated in COVE")]
        public bool Auto
        {
            get { return ((bool)(base.GetValue(CoveMessage.AutoProperty))); }
            set { base.SetValue(CoveMessage.AutoProperty, value); }
        }

        #endregion

        public CoveMessage()
		{
			//InitializeComponent();
		}

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            string strURI;

            strURI = "http://";
            if (string.IsNullOrEmpty(this.HostName))
            {
                Console.WriteLine("Unable to send messsage to COVE.\n");
                return base.Execute(executionContext);
            }

            strURI += (this.HostName + "/url?");
            if (!string.IsNullOrEmpty(this.Type))
                strURI += ("type=" + this.Type + "&");
            if (!string.IsNullOrEmpty(this.DataName))
                strURI += ("name=" + this.DataName + "&");
            if (!string.IsNullOrEmpty(this.Link))
                strURI += ("link=" + this.Link + "&");
            if (!string.IsNullOrEmpty(this.DataText))
                strURI += ("description=" + this.DataText + "&");
            if (this.Auto)
                strURI += ("auto=true&");
            strURI = strURI.Substring(0, strURI.Length - 1);

            Uri thisUri = new Uri(strURI);
            WebRequest webReq = HttpWebRequest.Create(thisUri);
            WebResponse webResp = null;
            try
            {
                webResp = webReq.GetResponse();
            }
            catch (Exception)
            {
                
            }
            if (webResp != null)
            {
                webResp.Close();
            }

            Console.WriteLine("Message sent to COVE.\n");

            return base.Execute(executionContext);
        }
	}
}
